package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.authoriseRFC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.User;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.authorizeRFCPanel.authorizeRFCPanelSubs.CreateWorkOrderDialog;

public class GetWorkorderDisplayDataThread extends Thread {

	private CreateWorkOrderDialog workOrderDialog;
	private DatabaseConnection connection;

	public GetWorkorderDisplayDataThread(CreateWorkOrderDialog workOrderDialog) {
		this.workOrderDialog = workOrderDialog;
	}

	@Override
	public void run() {

		this.connection = new DatabaseConnection();
		this.connection.connect();

		String[] listOfSystemAdministrators = getListOfSysadmins();

		this.connection.disconnect();
		this.workOrderDialog.setupWorkOrderDiyplay(listOfSystemAdministrators);
		this.workOrderDialog.getCreateWorkorder().setEnabled(true);
		this.workOrderDialog.getProgress().setVisible(false);
	}

	private String[] getListOfSysadmins() {
		ArrayList<String> list = new ArrayList<String>();

		String query = "SELECT * FROM `CMS_user` WHERE Rolle = 'usrSA'";

		ResultSet rs = connection.query(query);

		try {
			while (rs.next()) {

				list.add(rs.getString(User.TC_FIRSTNAMENAME)+ " "
						+ rs.getString(User.TC_NAME));

			}

		} catch (SQLException e) {

			String[] error = { "Fehler im Query" };
			return error;
		}

		String[] sysadmins = new String[list.size()];
		int i = 0;
		for (String string : list) {
			sysadmins[i++] = string;
		}
		return sysadmins;

	}

}
